<script setup lang="ts" name="MenuEdit">
import { MENU_TYPE_COLUMN, MENU_TYPE_ENUM } from "@/views/system/menu/const";
import { addMenuApi, getMenuApi, listMenuApi, updateMenuApi } from "@/api/system/menu";

const { proxy } = getCurrentInstance();

const { sys_is_not, sys_show_hide, sys_normal_disable } = useDict("sys_is_not", "sys_show_hide", "sys_normal_disable")

const emit = defineEmits(['closeDialog']);

const props = defineProps({
  id: {
    default: null,
  },
  parentId: {
    type: Number,
    default: null
  },
  title: {
    type: String,
    default: false,
  }
})

// 菜单管理表单
const menuFormRef = ref(null);

// 弹窗开关
const open = ref(false);

// 表单数据
const form = ref<MenuRecord>({});

// 菜单lieb
const menuList = ref<MenuRecord[]>([])

// 表单校验
const rules = ref({
  parentId: [
    { required: true, message: "上级菜单不能为空", trigger: "blur" },
  ],
  menuType: [
    { required: true, message: "菜单类型不能为空", trigger: "blur" },
  ],
  icon: [
    { required: true, message: "图标不能为空", trigger: "blur" },
  ],
  menuName: [
    { required: true, message: "菜单名称不能为空", trigger: "blur" },
  ],
  orderNum: [
    { required: true, message: "显示排序不能为空", trigger: "blur" },
  ],
  isFrame: [
    { required: true, message: "是否外链不能为空", trigger: "blur" },
  ],
  path: [
    { required: true, message: "路由地址不能为空", trigger: "blur" },
  ],
  component: [
    { required: true, message: "组件路径不能为空", trigger: "blur" },
  ],
  isCache: [
    { required: true, message: " 是否缓存不能为空", trigger: "blur" },
  ],
  visible: [
    { required: true, message: "显示状态不能为空", trigger: "blur" },
  ],
  status: [
    { required: true, message: "菜单状态不能为空", trigger: "blur" },
  ],
});

// 加载
const loading = ref(false);

/** 获取菜单管理详情 */
function getData() {
  reset();
  if (props.id || props.id === 0) {
    getMenuApi(props.id).then(response => {
      form.value = response.data;
      open.value = true;
    }).catch(() => {
      closeDialog(false);
    })
  } else {
    form.value.parentId = props.parentId;
    open.value = true;
  }
}

/** 获取菜单列表 */
function getMenuList() {
  listMenuApi().then(response => {
    menuList.value = [{
      menuId: 0,
      menuName: "主类目",
      children: handleTree(response.data, "menuId")
    }]
  });
}

/** 表单重置 */
function reset() {
  form.value = {
    menuId: null,
    parentId: null,
    menuType: MENU_TYPE_ENUM.menu,
    icon: null,
    menuName: null,
    orderNum: null,
    isFrame: IS_NOT_ENUM.not,
    path: null,
    component: null,
    perms: null,
    query: null,
    isCache: IS_NOT_ENUM.is,
    visible: SYS_SHOW_HIDE_ENUM.show,
    status: SYS_NORMAL_DISABLE_ENUM.normal,
    createTime: null,
  };
}

/** 关闭弹窗 */
function closeDialog(refresh: boolean) {
  open.value = false;
  emit("closeDialog", refresh);
}

/** 菜单类型变更 */
function menuTypeChange(value) {
  if (value != MENU_TYPE_ENUM.menu) {
    form.value.component = null;
  }
}

/** 提交按钮 */
function submitForm() {
  // @ts-ignore
  menuFormRef.value.validate((valid: boolean) => {
    if (valid) {
      loading.value = true;
      if (form.value.menuId) {
        updateMenuApi(form.value).then(() => {
          // @ts-ignore
          proxy.$modal.msgSuccess("修改成功");
          loading.value = false;
          closeDialog(true);
        }).catch(() => {
          loading.value = false
        });
      } else {
        addMenuApi(form.value).then(() => {
          // @ts-ignore
          proxy.$modal.msgSuccess("新增成功");
          loading.value = false;
          closeDialog(true);
        }).catch(() => {
          loading.value = false
        });
      }
    }
  })
}

watch(open, (val) => {
  if (!val) {
    closeDialog(false);
  }
})

getData();
getMenuList();
</script>

<template>
  <!-- 菜单管理编辑对话框 -->
  <el-dialog :title="title" v-model="open" width="740px" append-to-body :close-on-click-modal="false">
    <el-form ref="menuFormRef" :model="form" :rules="rules" :inline="true" label-width="96px">
      <el-form-item label="上级菜单" prop="parentId">
        <el-tree-select
            v-model="form.parentId"
            :data="menuList"
            :props="{ value: 'menuId', label: 'menuName', children: 'children' }"
            value-key="menuId"
            placeholder="选择上级菜单"
            check-strictly
        />
      </el-form-item>
      <el-form-item label="菜单类型" prop="menuType">
        <el-radio-group v-model="form.menuType" placeholder="请选择菜单类型">
          <el-radio
              :value="item.value"
              v-for="(item, index) in MENU_TYPE_COLUMN"
              :key="index"
              @change="menuTypeChange"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="图标" prop="icon" class="item-describe">
        <icon-select v-model="form.icon" placeholder="请输入图标" />
      </el-form-item>
      <el-form-item label="菜单名称" prop="menuName">
        <el-input v-model="form.menuName" placeholder="请输入菜单名称" />
      </el-form-item>
      <el-form-item label="显示排序" prop="orderNum">
        <el-input-number v-model="form.orderNum" :min="1" label="请输入显示排序" />
      </el-form-item>
      <el-form-item label="是否外链" prop="isFrame" v-if="form.menuType != MENU_TYPE_ENUM.button">
        <template #label>
          <span>
            <el-tooltip content="选择是外链则路由地址需要以`http(s)://`开头" placement="top">
              <icon-svg icon-name="QuestionFilled" />
            </el-tooltip>是否外链
          </span>
        </template>
        <el-radio-group v-model="form.isFrame" placeholder="请输入是否外链">
          <el-radio
              :value="item.value"
              v-for="(item, index) in sys_is_not"
              :key="index"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="路由地址" prop="path" v-if="form.menuType != MENU_TYPE_ENUM.button">
        <template #label>
          <span>
            <el-tooltip content="访问的路由地址，如：`user`，如外网地址需内链访问则以`http(s)://`开头" placement="top">
              <icon-svg icon-name="QuestionFilled" />
            </el-tooltip>
            路由地址
          </span>
        </template>
        <el-input v-model="form.path" placeholder="请输入路由地址" />
      </el-form-item>
      <el-form-item label="组件路径" prop="component" v-if="form.menuType == MENU_TYPE_ENUM.menu">
        <template #label>
          <span>
            <el-tooltip content="访问的组件路径，如：`system/user/index`，默认在`views`目录下" placement="top">
              <icon-svg icon-name="QuestionFilled" />
            </el-tooltip>
            组件路径
          </span>
        </template>
        <el-input v-model="form.component" placeholder="请输入组件路径" />
      </el-form-item>
      <el-form-item label="权限字符" prop="perms" v-if="form.menuType != MENU_TYPE_ENUM.folder">
        <template #label>
          <span>
            <el-tooltip content="控制器中定义的权限字符，如：system:user:list" placement="top">
              <icon-svg icon-name="QuestionFilled" />
            </el-tooltip>
            权限字符
          </span>
        </template>
        <el-input v-model="form.perms" placeholder="请输入权限字符" />
      </el-form-item>
      <el-form-item label=" 路由参数" prop="query" v-if="form.menuType == MENU_TYPE_ENUM.menu">
        <template #label>
          <span>
            <el-tooltip content='访问路由的默认传递参数，如：`{"id": 1, "name": "ry"}`' placement="top">
              <icon-svg icon-name="QuestionFilled" />
            </el-tooltip>
            路由参数
          </span>
        </template>
        <el-input v-model="form.query" placeholder="请输入路由参数" />
      </el-form-item>
      <el-form-item label=" 是否缓存" prop="isCache" v-if="form.menuType == MENU_TYPE_ENUM.menu">
        <template #label>
          <span>
            <el-tooltip content="选择是则会被`keep-alive`缓存，需要匹配组件的`name`和地址保持一致" placement="top">
              <icon-svg icon-name="QuestionFilled" />
            </el-tooltip>
            是否缓存
          </span>
        </template>
        <el-radio-group v-model="form.isCache">
          <el-radio
              :value="item.value"
              v-for="(item, index) in sys_is_not"
              :key="index"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="显示状态" prop="visible" v-if="form.menuType != MENU_TYPE_ENUM.button">
        <template #label>
          <span>
            <el-tooltip content="选择隐藏则路由将不会出现在侧边栏，但仍然可以访问" placement="top">
              <icon-svg icon-name="QuestionFilled" />
            </el-tooltip>
            显示状态
          </span>
        </template>
        <el-radio-group v-model="form.visible">
          <el-radio
              :value="item.value"
              v-for="(item, index) in sys_show_hide"
              :key="index"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="菜单状态" prop="status" v-if="form.menuType != MENU_TYPE_ENUM.button">
        <template #label>
          <span>
            <el-tooltip content="选择停用则路由将不会出现在侧边栏，也不能被访问" placement="top">
              <icon-svg icon-name="QuestionFilled" />
            </el-tooltip>
            菜单状态
          </span>
        </template>
        <el-radio-group v-model="form.status">
          <el-radio
              :value="item.value"
              v-for="(item, index) in sys_normal_disable"
              :key="index"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" v-debounce="submitForm" :loading="loading">确 定</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
.el-form {
  :deep(.el-form-item) {
    .el-form-item__content {
      width: 226px;
    }
  }
}
</style>
  